package client.XMLLoaders;

import java.util.ArrayList;

import org.xml.sax.Attributes;

import client.Creature;
import client.abilities.AbilityLookup;
import client.abilities.CreatureAbility;
import client.MagicColor;
import client.SAXReader;

public class CreatureLoader extends SAXReader {

	private ArrayList<Creature> creatures;
	private Creature currentCreature;
	private CreatureAbility currentAbility;
	private String currentAbilityName;

	public CreatureLoader(String filePath) {
		super();
		creatures = new ArrayList<Creature>();
		super.start(filePath);
	}

	public ArrayList<Creature> getCreatures() {
		return creatures;
	}

	public void startElement(String uri, String name, String qName,
			Attributes atts) {
		if (qName.equals("creature")) {
			currentCreature = new Creature();
		} else {

		}
		// System.out.println("Att: " + atts.getValue("id"));
	}

	public void endElement(String uri, String name, String qName) {
		if (qName.equals("cname")) {
			currentCreature.setName(currentData);
		} else if (qName.equals("color")) {
			currentCreature.setColor(MagicColor.valueOf(currentData));
		} else if (qName.equals("type")) {
			currentCreature.setCreatureType(currentData);
		} else if (qName.equals("strength")) {
			currentCreature.setStrength(Integer.parseInt(currentData));
		} else if (qName.equals("maxHP")) {
			currentCreature.setMaxHP(Integer.parseInt(currentData));
			currentCreature.setCurrentHP(currentCreature.getMaxHP());
		} else if (qName.equals("accuracy")) {
			currentCreature.setAccuracy(Integer.parseInt(currentData));
		} else if (qName.equals("speed")) {
			currentCreature.setSpeed(Integer.parseInt(currentData));
		} else if (qName.equals("strengthAt100")) {
			currentCreature.setStrengthAt100(Integer.parseInt(currentData));
		} else if (qName.equals("maxHPAt100")) {
			currentCreature.setMaxHPAt100(Integer.parseInt(currentData));
			currentCreature.setCurrentHP(currentCreature.getMaxHP());
		} else if (qName.equals("accuracyAt100")) {
			currentCreature.setAccuracyAt100(Integer.parseInt(currentData));
		} else if (qName.equals("speedAt100")) {
			currentCreature.setSpeedAt100(Integer.parseInt(currentData));
		} else if (qName.equals("maxEquipment")) {
			currentCreature.setMaxEquipment(Integer.parseInt(currentData));
		} else if (qName.equals("criticalChance")) {
			currentCreature.setCriticalChance(Integer.parseInt(currentData));
		} else if (qName.equals("name")) {
			currentAbilityName = currentData;
		} else if (qName.equals("tier")) {
			CreatureAbility ability = AbilityLookup.lookup(currentAbilityName, Integer.parseInt(currentData));
			currentAbility = ability;
		} else if (qName.equals("learned")) {
			currentAbility.setLearned(Integer.parseInt(currentData));
			currentCreature.addAbility(currentAbility);
		} else if (qName.equals("creature")) {
			creatures.add(currentCreature);
		}
	}

	public void characters(char ch[], int start, int length) {
		super.characters(ch, start, length);
	}
}
